#include "ExtraCreditRBJ.h"
#include "Store_menu.h"
#include <algorithm>
#include "Graph.h"
#include "Window.h"
#include <map>
using namespace std;
using namespace Store;

ExtraCreditRBJ::ExtraCreditRBJ() { };

extern Store_menu m;

vector<vector<Cat_Percent> > ExtraCreditRBJ::graph_display(){	
	vector<map<int,double> >cat_sales(12);
	
	map<int,int> cart_month_map;
	for(int i=0; i<m.get_carts().size(); i++){
		cart_month_map[m.get_carts()[i].getCartId()] = m.get_carts()[i].getPurchaseDate().month();
	}

	map<int,int> prod_price_map;
	for(int i=0; i<m.get_prods().size(); i++){
		prod_price_map[m.get_prods()[i].getProdId()] = m.get_prods()[i].getProdPrice();
	}

	map<int,int> prod_cat_map;
	for(int i=0; i<m.get_prods().size(); i++){
		prod_cat_map[m.get_prods()[i].getProdId()] = m.get_prods()[i].getCatId();
	}
	
	for(int i=0; i<12; i++){
		map<int,double> cat_sales_map;
		for(int j=0; j<m.get_cats().size(); j++){
			cat_sales_map[m.get_cats()[j].getCatId()] = 0;
		}
		for(int j=0; j<m.get_cart_items().size(); j++){
			if(cart_month_map[m.get_cart_items()[j].getCartId()]==(i+1)){
				cat_sales_map[prod_cat_map[m.get_cart_items()[j].getProdId()]] += m.get_cart_items()[j].getProdQty() * prod_price_map[m.get_cart_items()[j].getProdId()];
			}
		}
		cat_sales[i] = cat_sales_map;
	}

	vector<double> total_per_month(12);
	for(int i=0; i<12; i++){
		total_per_month[i] = 0;
	}

	for(int i=0; i<cat_sales.size(); i++){
		for(int j=0; j<m.get_cats().size(); j++){
			if(cat_sales[i][m.get_cats()[j].getCatId()]!=0)
				total_per_month[i] += cat_sales[i][m.get_cats()[j].getCatId()];			
		}
	}
	
	vector<vector<Cat_Percent> > result(12);
	for(int i=0; i<cat_sales.size(); i++){
		cout << "-------month " << i << endl;
		vector<Cat_Percent> per_cat; 
		for(int j=0; j<m.get_cats().size(); j++){
			if(cat_sales[i][m.get_cats()[j].getCatId()]!=0){
				Cat_Percent cat_percent;
				cat_percent.cat_id = m.get_cats()[j].getCatId();
				cat_percent.percent = cat_sales[i][m.get_cats()[j].getCatId()]/total_per_month[i]*100;
				cout << "cat_id = " << cat_percent.cat_id << endl;
				cout << "percent = " << cat_percent.percent << endl;
				per_cat.push_back(cat_percent);
			}
		}
		result[i] = per_cat;
		cout << endl;
	}

	return result;
}
		
	